package com.cn.service.server.Equipment.Impl;

import com.cn.bean.pojo.PartsManagement.BusinessTypeTable;
import com.cn.bean.pojo.bc.K3bm;
import com.cn.bean.pojo.lbj.ComponentCategory;
import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.bean.vo.Equipment.*;
import com.cn.bean.vo.PartsManagement.BusinessProjectTypeMapping;
import com.cn.bean.vo.PartsManagement.ComponentItemClassification;
import com.cn.bean.vo.PartsManagement.EquipmentBasicInfo;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db1.bc.K3bmDao;
import com.cn.mapper.dao.db5.Equipment.ComponentBasicInformationDao;
import com.cn.mapper.dao.db5.Equipment.ComponentStructureGroupCompositionDao;
import com.cn.mapper.dao.db5.Equipment.EquipmentComponentMaterialMappingSettingDao;
import com.cn.mapper.dao.db5.PartsManagement.*;
import com.cn.mapper.dao.db5.Equipment.EquipmentStructureDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.Equipment.EquipmentStructureService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class EquipmentStructureServiceImpl implements EquipmentStructureService {
    private final EquipmentStructureDao equipmentStructureDao;
    private final BusinessProjectTypeMappingDao businessProjectTypeMappingDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;
    //table2
    private final EquipmentComponentMaterialMappingSettingDao equipmentComponentMaterialMappingSettingDao;
    //table1
    private final ComponentStructureGroupCompositionDao componentStructureGroupCompositionDao;
    private final ComponentCategoryDao componentCategoryDao;
    private final ComponentBasicInformationDao componentBasicInformationDao;
    private final ComponentItemClassificationDao componentItemClassificationDao;
    private final K3bmDao k3bmDao;
    private final EquipmentBasicInfoDao equipmentBasicInfoDao;
    private final BusinessTypeDao businessTypeDao;

    @Autowired(required = false)
    public EquipmentStructureServiceImpl(BusinessTypeDao businessTypeDao,ComponentCategoryDao componentCategoryDao,EquipmentBasicInfoDao equipmentBasicInfoDao, K3bmDao k3bmDao, ComponentItemClassificationDao componentItemClassificationDao, ComponentBasicInformationDao componentBasicInformationDao, ComponentStructureGroupCompositionDao componentStructureGroupCompositionDao, EquipmentComponentMaterialMappingSettingDao equipmentComponentMaterialMappingSettingDao, EquipmentStructureDao equipmentStructureDao, Sys_Sequence_Info_TDAO sysSequenceInfoTdao, BusinessProjectTypeMappingDao businessProjectTypeMappingDao) {
        this.equipmentStructureDao = equipmentStructureDao;
        this.sysSequenceInfoTdao = sysSequenceInfoTdao;
        this.businessProjectTypeMappingDao = businessProjectTypeMappingDao;
        this.equipmentComponentMaterialMappingSettingDao = equipmentComponentMaterialMappingSettingDao;
        this.componentStructureGroupCompositionDao = componentStructureGroupCompositionDao;
        this.componentBasicInformationDao = componentBasicInformationDao;
        this.componentItemClassificationDao = componentItemClassificationDao;
        this.k3bmDao = k3bmDao;
        this.equipmentBasicInfoDao = equipmentBasicInfoDao;
        this.componentCategoryDao = componentCategoryDao;
        this.businessTypeDao = businessTypeDao;
    }

    @Override
    public List<EquipmentStructure> selectEquipmentStructure(int limit, int page, String structureCode, String structureName) {
        List<EquipmentStructure> equipmentStructures = equipmentStructureDao.selectEquipmentStructure(limit, page, structureCode, structureName);
        for (EquipmentStructure eq : equipmentStructures) {
//            List<BusinessProjectTypeMapping> bus = businessProjectTypeMappingDao.selectBusinessProjectId(eq.getBusinessTypeID());
            List<BusinessTypeTable> bus = businessTypeDao.selectBusinessTypeId(eq.getBusinessTypeID());
            eq.setBusinessTypeName(bus.get(0).getName());
        }
        return equipmentStructures;
    }

    @Override
    public List<EquipmentStructure> selectEquipmentStructureChildAll(int ID) {
        List<EquipmentStructure> equipmentStructures = equipmentStructureDao.selectEquipmentStructureChildAll(ID);
        for (EquipmentStructure eqi : equipmentStructures) {
            List<ComponentItemClassification> componentItemClassifications = componentItemClassificationDao.selectChildID(eqi.getComponentID());
            String cName = componentItemClassifications.get(0).getCName();

            eqi.setComponentName(cName);
        }
        return equipmentStructures;
    }

    @Override
    public Result addEquipmentStructure(int businessTypeID, String structureName, int versionStatus, String description, int categoryID, int isSplit, int isSplitLeftRight, String replacementCycleMethod, String cycleDays, BigDecimal replacementCycleDailyOutput, int partAttributes, int upperCount, int lowerCount, int leftCount, int rightCount, int upperLeftCount, int upperRightCount, int lowerLeftCount, int lowerRightCount, int quantity, String measurementUnit) {
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("EquipmentStructureMainTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "EquipmentStructureMainTable");
        equipmentStructureDao.addEquipmentStructure(ID, businessTypeID, code, structureName, versionStatus, "1", description);
        Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("EquipmentComponentStructureTable");
        int presentValue1 = businessTypeTable1.getPresentValue();
        int stepLength1 = businessTypeTable1.getStepLength();
        int ID1 = presentValue1 + stepLength1;
        String code1 = String.valueOf(ID1);
        if (ID1 > businessTypeTable1.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "EquipmentComponentStructureTable");
        //零件ID componentID    就是businessTypeID
        equipmentStructureDao.addEquipmentStructureChile(ID1, ID, businessTypeID, categoryID, isSplit, isSplitLeftRight, replacementCycleMethod, cycleDays, replacementCycleDailyOutput, partAttributes, upperCount, lowerCount, leftCount, rightCount, upperLeftCount, upperRightCount, lowerLeftCount, lowerRightCount, quantity, measurementUnit);
        return Result.success("200", "提交成功");
    }

    @Override
    public List<EquipmentStructure> selectEquipmentStructureID(int businessTypeID, String structureName, int versionStatus, String description) {
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("EquipmentStructureMainTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        int equipmentStructureMainTable = sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "EquipmentStructureMainTable");
        int i = equipmentStructureDao.addEquipmentStructure(ID, businessTypeID, code, structureName, versionStatus, "1", description);
        List<EquipmentStructure> equipmentStructures = equipmentStructureDao.selectEquipmentStructureID(ID);
        return equipmentStructures;
    }

    @Override
    public Result addEquipmentStructureChileChild(EquipmentStructure equipmentStructure, List<ComponentStructureGroupComposition> tableData1, List<EquipmentComponentMaterialMappingSetting> tableData2, List<ComponentBasicInformation> tableData3) {
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("EquipmentComponentStructureTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "EquipmentComponentStructureTable");
        equipmentStructure.setCID(ID);
        int i = equipmentStructureDao.addEquipmentStructureChileChild(equipmentStructure);
        if (i == 0) {
            Result.failure("500", "保存失败");
        }
        for (ComponentStructureGroupComposition comp : tableData1) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("Component_StructureGroup_Composition");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "Component_StructureGroup_Composition");
            componentStructureGroupCompositionDao.addComponentStructureGroupComposition(ID1, ID, equipmentStructure.getComponentID(), comp.getGroupNumber(), comp.getQuantityWithinGroup(), comp.getUniversalBetweenGroups());
        }

        for (EquipmentComponentMaterialMappingSetting eqc : tableData2) {
            Sys_Sequence_Info_T businessTypeTable2 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("EquipmentComponentMaterialMappingSetting");
            int presentValue2 = businessTypeTable2.getPresentValue();
            int stepLength2 = businessTypeTable2.getStepLength();
            int ID2 = presentValue2 + stepLength2;
            String code1 = String.valueOf(ID2);
            if (ID2 > businessTypeTable2.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID2, "EquipmentComponentMaterialMappingSetting");
            equipmentComponentMaterialMappingSettingDao.addEquipmentComponentMaterialMappingSetting(ID2, ID, equipmentStructure.getComponentID(), eqc.getMaterialID(), eqc.getDefaultMaterial(), eqc.getMaterialQuantity());

        }
        for (ComponentBasicInformation cob : tableData3) {
            Sys_Sequence_Info_T businessTypeTable3 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("ComponentBasicInformationTable");
            int presentValue3 = businessTypeTable3.getPresentValue();
            int stepLength3 = businessTypeTable3.getStepLength();
            int ID3 = presentValue3 + stepLength3;
            String code1 = String.valueOf(ID3);
            if (ID3 > businessTypeTable3.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID3, "ComponentBasicInformationTable");
            cob.setID(ID3);
            cob.setEquipmentStructureId(ID);
            cob.setComponentId(equipmentStructure.getComponentID());
            cob.setConsumableCategory(equipmentStructure.getConsumableCategory());
            cob.setM0Id(equipmentStructure.getID());

            componentBasicInformationDao.addComponentBasicInformation(cob);
        }
        return Result.success("200", "保存成功");
    }

    @Override
    public Result deleteEquipmentStructure(List<EquipmentStructure> idList) {
        for (EquipmentStructure equipmentStructure : idList) {
            Integer ID = equipmentStructure.getID();
            //获取所有子表
            List<EquipmentStructure> equipmentStructures = equipmentStructureDao.selectEquipmentComponentStructureTableID(ID);
            for (EquipmentStructure equ : equipmentStructures) {
                int componentID = equ.getComponentID();
                int cid = equ.getCID();
                componentBasicInformationDao.deleteComponentBasicInformation(cid);
                componentStructureGroupCompositionDao.deleteComponentStructureGroupComposition(cid);
                equipmentComponentMaterialMappingSettingDao.deleteEquipmentComponentMaterialMappingSetting(cid);
            }
            equipmentStructureDao.deleteEquipmentStructure(ID);
            equipmentStructureDao.deleteEquipmentStructureChild(ID);

        }

        return Result.success("200", "删除成功");
    }

    @Override
    public Result deleteEquipmentStructureChileChild(int id) {
        componentBasicInformationDao.deleteComponentBasicInformation(id);
        componentStructureGroupCompositionDao.deleteComponentStructureGroupComposition(id);
        equipmentComponentMaterialMappingSettingDao.deleteEquipmentComponentMaterialMappingSetting(id);
        equipmentStructureDao.deleteEquipmentStructureChildID(id);

        return Result.success("200", "删除成功");
    }

    @Override
    public List<EquipmentStructure> selectEquipmentComponeChildID(int ID) {
        List<EquipmentStructure> equipmentStructures = equipmentStructureDao.selectEquipmentComponeChildID(ID);
        List<ComponentBasicInformation> componentBasicInformations = componentBasicInformationDao.selectComponentBasicInformationMID(ID);
        List<ComponentStructureGroupComposition> componentStructureGroupCompositions = componentStructureGroupCompositionDao.selectComponentStructureGroupCompositionMID(ID);
        List<EquipmentComponentMaterialMappingSetting> equipmentComponentMaterialMappingSettings = equipmentComponentMaterialMappingSettingDao.selectEquipmentComponentMaterialMappingSettingM0Id(ID);
        for (EquipmentComponentMaterialMappingSetting em : equipmentComponentMaterialMappingSettings) {
            List<K3bm> k3bms = k3bmDao.selectK3bmId(em.getMaterialID());
            em.setK3Code(k3bms.get(0).getFnumber());
            em.setK3Name(k3bms.get(0).getBmmc());
        }
        for (EquipmentStructure equipmentStructure : equipmentStructures) {
            //类别名称
            List<ComponentCategory> componentCategories = componentCategoryDao.selectComponentMechanicalUsageCategoryId(equipmentStructure.getCategoryID());
            equipmentStructure.setCategoryName(componentCategories.get(0).getName());
//            List<BusinessProjectTypeMapping> bus = businessProjectTypeMappingDao.selectBusinessProjectId(equipmentStructure.getCategoryID());
//            equipmentStructure.setCategoryName(bus.get(0).getBusinessTypeName());



            //零件名称
            List<ComponentItemClassification> componentItemClassifications = componentItemClassificationDao.selectChildID(equipmentStructure.getComponentID());
            String cName = componentItemClassifications.get(0).getCName();
            equipmentStructure.setComponentName(cName);

            equipmentStructure.setComponentBasicInformationList(componentBasicInformations);
            equipmentStructure.setEquipmentComponentMaterialMappingSettingList(equipmentComponentMaterialMappingSettings);
            equipmentStructure.setComponentStructureGroupCompositionsList(componentStructureGroupCompositions);
        }
        return equipmentStructures;
    }

    @Override
    public Result upDateEquipmentStructure(EquipmentStructure equipmentStructure, List<ComponentStructureGroupComposition> tableData1, List<EquipmentComponentMaterialMappingSetting> tableData2, List<ComponentBasicInformation> tableData3) {
        //判断这个结构是否被引用了
//        Integer integer = equipmentBasicInfoDao.selectEq(equipmentStructure.getM0ID());
//        if (integer>0){
//            return Result.failure("500","该结构已被引用，不能修改");
//        }
        int cid = equipmentStructure.getCID();
        componentBasicInformationDao.deleteComponentBasicInformation(cid);
        componentStructureGroupCompositionDao.deleteComponentStructureGroupComposition(cid);
        equipmentComponentMaterialMappingSettingDao.deleteEquipmentComponentMaterialMappingSetting(cid);
        equipmentStructureDao.deleteEquipmentStructureChildID(cid);
        int i = equipmentStructureDao.addEquipmentStructureChileChild(equipmentStructure);

        if (i == 0) {
            Result.failure("500", "保存失败");
        }
        for (ComponentStructureGroupComposition comp : tableData1) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("Component_StructureGroup_Composition");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "Component_StructureGroup_Composition");
            componentStructureGroupCompositionDao.addComponentStructureGroupComposition(ID1, cid, equipmentStructure.getComponentID(), comp.getGroupNumber(), comp.getQuantityWithinGroup(), comp.getUniversalBetweenGroups());
        }

        for (EquipmentComponentMaterialMappingSetting eqc : tableData2) {
            Sys_Sequence_Info_T businessTypeTable2 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("EquipmentComponentMaterialMappingSetting");
            int presentValue2 = businessTypeTable2.getPresentValue();
            int stepLength2 = businessTypeTable2.getStepLength();
            int ID2 = presentValue2 + stepLength2;
            String code1 = String.valueOf(ID2);
            if (ID2 > businessTypeTable2.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID2, "EquipmentComponentMaterialMappingSetting");
            equipmentComponentMaterialMappingSettingDao.addEquipmentComponentMaterialMappingSetting(ID2, cid, equipmentStructure.getComponentID(), eqc.getMaterialID(), eqc.getDefaultMaterial(), eqc.getMaterialQuantity());

        }
        for (ComponentBasicInformation cob : tableData3) {
            Sys_Sequence_Info_T businessTypeTable3 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("ComponentBasicInformationTable");
            int presentValue3 = businessTypeTable3.getPresentValue();
            int stepLength3 = businessTypeTable3.getStepLength();
            int ID3 = presentValue3 + stepLength3;
            String code1 = String.valueOf(ID3);
            if (ID3 > businessTypeTable3.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID3, "ComponentBasicInformationTable");
            cob.setID(ID3);
            cob.setEquipmentStructureId(cid);
            cob.setComponentId(equipmentStructure.getComponentID());
            cob.setConsumableCategory(equipmentStructure.getConsumableCategory());
            cob.setM0Id(equipmentStructure.getID());
            componentBasicInformationDao.addComponentBasicInformation(cob);
        }
        return Result.success("200", "修改成功");
    }

    @Override
    public Result updateEquipmentStructureMain(int ID, int businessTypeID, String structureName, int versionStatus, String version, String description) {

        int i = equipmentStructureDao.updateEquipmentStructureMain(ID, businessTypeID, structureName, versionStatus, version, description);
        if (i == 0) {
            return Result.failure("500", "修改失败");
        }

        return Result.success("200", "修改成功");
    }

    @Override
    public List<EquipmentStructure> selectAllMain(int id) {
        List<EquipmentStructure> equipmentStructures = equipmentStructureDao.selectEquipmentStructureID(id);
        return equipmentStructures;
    }

    @Override
    public List<EquipmentStructure> selectEquipmentStructureMainAuto1() {
        return equipmentStructureDao.selectEquipmentStructureMainAuto1();
    }

    @Override
    public List<EquipmentStructure> selectEquipmentStructureID(int id) {
        EquipmentBasicInfo equipmentBasicInfo = equipmentStructureDao.selectEquipmentMechanicalStructureMapping(id);
        return equipmentStructureDao.selectEquipmentStructureID(equipmentBasicInfo.getStructureId());
    }

}
